package cn.iocoder.yudao.module.contract.dal.mysql.order;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.contract.controller.admin.exportContract.vo.ExportOrderPageReqVO;
import cn.iocoder.yudao.module.contract.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.orderContract.OrderContractDO;
import cn.iocoder.yudao.module.contract.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.contract.controller.admin.order.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 订单 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface OrderMapper extends BaseMapperX<OrderDO> {

    default PageResult<OrderDO> selectPage(OrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrderDO>()
//                .eqIfPresent(OrderDO::getContractTypeCode, reqVO.getContractTypeCode())
//                .likeIfPresent(OrderDO::getContractTypeName, reqVO.getContractTypeName())
//                .eqIfPresent(OrderDO::getContractTemplateId, reqVO.getContractTemplateId())
//                .likeIfPresent(OrderDO::getContractTemplateName, reqVO.getContractTemplateName())
                .eqIfPresent(OrderDO::getAudat, reqVO.getAudat())
                .likeIfPresent(OrderDO::getVbeln, reqVO.getVbeln())
//                .likeIfPresent(OrderDO::getAuart, reqVO.getAuart())
//                .likeIfPresent(OrderDO::getAuartt, reqVO.getAuartt())
                .likeIfPresent(OrderDO::getVkorg, reqVO.getVkorg())
//                .likeIfPresent(OrderDO::getBukrs, reqVO.getBukrs())
//                .likeIfPresent(OrderDO::getButxt, reqVO.getButxt())
//                .likeIfPresent(OrderDO::getKunnr, reqVO.getKunnr())
//                .likeIfPresent(OrderDO::getKname, reqVO.getKname())
//                .likeIfPresent(OrderDO::getVkbur, reqVO.getVkbur())
//                .likeIfPresent(OrderDO::getVkburt, reqVO.getVkburt())
//                .likeIfPresent(OrderDO::getVkgrp, reqVO.getVkgrp())
//                .likeIfPresent(OrderDO::getVkgrpt, reqVO.getVkgrpt())
//                .likeIfPresent(OrderDO::getYwy, reqVO.getYwy())
//                .likeIfPresent(OrderDO::getYwyt, reqVO.getYwyt())
//                .likeIfPresent(OrderDO::getVsbed, reqVO.getVsbed())
//                .likeIfPresent(OrderDO::getZzhtgklx, reqVO.getZzhtgklx())
//                .likeIfPresent(OrderDO::getZzhtgklxms, reqVO.getZzhtgklxms())
//                .likeIfPresent(OrderDO::getZzhthbz, reqVO.getZzhthbz())
//                .likeIfPresent(OrderDO::getZzshdz, reqVO.getZzshdz())
//                .likeIfPresent(OrderDO::getZterm, reqVO.getZterm())
//                .likeIfPresent(OrderDO::getZtermt, reqVO.getZtermt())
//                .likeIfPresent(OrderDO::getLifsk, reqVO.getLifsk())
//                .likeIfPresent(OrderDO::getZmonth, reqVO.getZmonth())
//                .likeIfPresent(OrderDO::getAugru, reqVO.getAugru())
//                .likeIfPresent(OrderDO::getAugrut, reqVO.getAugrut())
                .betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
                .and(!ObjectUtils.isEmpty(reqVO.getPzinfo()),i -> i.like(OrderDO::getAuart, reqVO.getPzinfo()).or().like(OrderDO::getAuartt, reqVO.getPzinfo()))
                .and(!ObjectUtils.isEmpty(reqVO.getBuinfo()),i -> i.like(OrderDO::getBukrs, reqVO.getBuinfo()).or().like(OrderDO::getButxt, reqVO.getBuinfo()))
                .and(!ObjectUtils.isEmpty(reqVO.getKuinfo()),i -> i.like(OrderDO::getKunnr, reqVO.getKuinfo()).or().like(OrderDO::getKname, reqVO.getKuinfo()))
                .and(!ObjectUtils.isEmpty(reqVO.getVkinfo()),i -> i.like(OrderDO::getVkbur, reqVO.getVkinfo()).or().like(OrderDO::getVkburt, reqVO.getVkinfo()))
                .and(!ObjectUtils.isEmpty(reqVO.getVkginfo()),i -> i.like(OrderDO::getVkgrp, reqVO.getVkginfo()).or().like(OrderDO::getVkgrpt, reqVO.getVkginfo()))
                .and(!ObjectUtils.isEmpty(reqVO.getYwyinfo()),i -> i.like(OrderDO::getYwy, reqVO.getYwyinfo()).or().like(OrderDO::getYwyt, reqVO.getYwyinfo()))
                .ne(OrderDO::getHtdymb,"Y4")
                .orderByDesc(OrderDO::getAudat).orderByDesc(OrderDO::getVbeln));
    }

    default PageResult<OrderDO> getExportOrderPage(ExportOrderPageReqVO reqVO) {
        return selectPage(reqVO, new MPJLambdaWrapperX<OrderDO>()
                .between(!ObjectUtils.isEmpty(reqVO.getAudat()),OrderDO::getAudat, ArrayUtils.get(reqVO.getAudat(), 0),ArrayUtils.get(reqVO.getAudat(), 1))
                .like(!ObjectUtils.isEmpty(reqVO.getVbeln()) && "1".equals(reqVO.getVbelnType()),OrderDO::getVbeln, reqVO.getVbeln())
                .in(!ObjectUtils.isEmpty(reqVO.getVbeln()) && "2".equals(reqVO.getVbelnType()),OrderDO::getVbeln, !ObjectUtils.isEmpty(reqVO.getVbeln()) ?reqVO.getVbeln().split(","): new Object[]{""})

                .like(!ObjectUtils.isEmpty(reqVO.getZtermt()) && "1".equals(reqVO.getZtermtType()),OrderDO::getZtermt, reqVO.getZtermt())
                .in(!ObjectUtils.isEmpty(reqVO.getZtermt()) && "2".equals(reqVO.getZtermtType()),OrderDO::getZtermt, !ObjectUtils.isEmpty(reqVO.getZtermt()) ?reqVO.getZtermt().split(","): new Object[]{""})
                .notIn(!ObjectUtils.isEmpty(reqVO.getZtermt()) && "3".equals(reqVO.getZtermtType()),OrderDO::getZtermt, !ObjectUtils.isEmpty(reqVO.getZtermt()) ?reqVO.getZtermt().split(","): new Object[]{""})

                .ge(!ObjectUtils.isEmpty(reqVO.getVbelnStart()),OrderDO::getVbeln, reqVO.getVbelnStart())
                .le(!ObjectUtils.isEmpty(reqVO.getVbelnEnd()),OrderDO::getVbeln, reqVO.getVbelnEnd())
                .like(!ObjectUtils.isEmpty(reqVO.getVkorg()),OrderDO::getVkorg, reqVO.getVkorg())
                .in(!ObjectUtils.isEmpty(reqVO.getZzhtgklxms()),OrderDO::getZzhtgklxms,  !ObjectUtils.isEmpty(reqVO.getZzhtgklxms()) ?reqVO.getZzhtgklxms().split(","): new Object[]{""})
                .in(!ObjectUtils.isEmpty(reqVO.getPzinfo()),OrderDO::getAuart,  !ObjectUtils.isEmpty(reqVO.getPzinfo()) ?reqVO.getPzinfo().split(","): new Object[]{""})
                .in(!ObjectUtils.isEmpty(reqVO.getBuinfo()),OrderDO::getButxt,  !ObjectUtils.isEmpty(reqVO.getBuinfo()) ?reqVO.getBuinfo().split(","): new Object[]{""})
//                .in(!ObjectUtils.isEmpty(reqVO.getKuinfo()),OrderDO::getKname,  !ObjectUtils.isEmpty(reqVO.getKuinfo()) ?reqVO.getKuinfo().split(","): new Object[]{""})

                .and(!ObjectUtils.isEmpty(reqVO.getKunnr()) && "1".equals(reqVO.getKunnrType()),i-> i.like(OrderDO::getKunnr, reqVO.getKunnr()).or().like(OrderDO::getKname, reqVO.getKunnr()))
                .and(!ObjectUtils.isEmpty(reqVO.getKunnr()) && "2".equals(reqVO.getKunnrType()),i -> i.in(OrderDO::getKunnr, !ObjectUtils.isEmpty(reqVO.getKunnr()) ?reqVO.getKunnr().split(","): new Object[]{""}).or().in(OrderDO::getKname, !ObjectUtils.isEmpty(reqVO.getKunnr()) ?reqVO.getKunnr().split(","): new Object[]{""}))

                .in(!ObjectUtils.isEmpty(reqVO.getVkinfo()),OrderDO::getVkburt,  !ObjectUtils.isEmpty(reqVO.getVkinfo()) ?reqVO.getVkinfo().split(","): new Object[]{""})
                .and(!ObjectUtils.isEmpty(reqVO.getVkginfo()),i -> i.like(OrderDO::getVkgrp, reqVO.getVkginfo()).or().like(OrderDO::getVkgrpt, reqVO.getVkginfo()))
                .in(!ObjectUtils.isEmpty(reqVO.getYwyinfo()),OrderDO::getYwyt,  !ObjectUtils.isEmpty(reqVO.getYwyinfo()) ?reqVO.getYwyinfo().split(","): new Object[]{""})

                .in(!ObjectUtils.isEmpty(reqVO.getZzhtgklx()),OrderDO::getZzhtgklx,  !ObjectUtils.isEmpty(reqVO.getZzhtgklx()) ?reqVO.getZzhtgklx().split(","): new Object[]{""})
                .in(!ObjectUtils.isEmpty(reqVO.getZterm()),OrderDO::getZterm,  !ObjectUtils.isEmpty(reqVO.getZterm()) ?reqVO.getZterm().split(","): new Object[]{""})
                .eq(!ObjectUtils.isEmpty(reqVO.getStatus()), OrderDO::getStatus, reqVO.getStatus())

                .eq(OrderDO::getHtdymb,"Y4")

                .orderByDesc(ObjectUtils.isEmpty(reqVO.getOrder()),OrderDO::getAudat).orderByDesc(ObjectUtils.isEmpty(reqVO.getOrder()),OrderDO::getVbeln)
                .orderBy(ObjectUtils.isNotEmpty(reqVO.getOrder()),"ascending".equals(reqVO.getOrder()), StringUtils.toUnderScoreCase(reqVO.getProp()))
        );
    }

    int updateOrderShippingStatus(@Param("vbeln") String vbeln, @Param("latestShipmentDate") String latestShipmentDate,@Param("completeState") String completeState,@Param("completeStateExplain") String completeStateExplain);
}